import 'package:cbec_app/components/decoration/video_controls.dart';
import 'package:cbec_app/utils/logger.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:video_player/video_player.dart';

///店铺装修-视频
class DecorationVideo extends StatefulWidget {
  ///视频配置
  final Map<String, dynamic> info;
  const DecorationVideo({super.key, required this.info});

  @override
  State<StatefulWidget> createState() {
    return _DecorationVideoState();
  }
}

class _DecorationVideoState extends State<DecorationVideo> {
  late VideoPlayerController _controller;
  @override
  void initState() {
    super.initState();
    Uri sourceUri = Uri.parse(widget.info['customUrl']!=''?widget.info['customUrl']:widget.info['selectUrl']);
    _controller = VideoPlayerController.networkUrl(sourceUri);
    _controller.addListener(() {
      setState(() {});
    });
    _controller.setLooping(false);
    _controller.initialize().then((_) => setState(() {}));
    if(widget.info['autoPlay']==1){
      //自动播放
      // _controller.play();
    }
    
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.only(left: getMargin(), right: getMargin()),
      height: 200.h,
      child: AspectRatio(
        aspectRatio: _controller.value.aspectRatio,
        child: Stack(
          alignment: Alignment.bottomCenter,
          children: [
            VideoPlayer(_controller),
            VideoControls(controller: _controller,info: widget.info,),
            VideoProgressIndicator(_controller, allowScrubbing: true),
          ],
        ),
      ),
    );
  }

  ///获取页面边距
  double getMargin() {
    return (widget.info['pagePadding'] as int).toDouble();
  }
  ///获取可用宽度
  double getUseWidth() {
    return 375.w - getMargin() * 2;
  }
}
